package com.sandu.mirrorforfamily.ui.home

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.RemoteException
import android.telephony.CarrierConfigManager.ImsVoice
import android.util.Log
import com.sandu.lib_common.base.BaseVBFragment
import com.sandu.lib_mvvm.base.viewmodel.BaseViewModel
import com.sandu.lib_mvvm.ext.util.TAG
import com.sandu.mirrorforfamily.databinding.FragmentHomeBinding
import com.sandu.voiceassistant.IChangeCallback
import com.sandu.voiceassistant.IStudentInterface
import com.sandu.voiceassistant.IVoiceInterface
import com.sandu.voiceassistant.StudentInfo
import com.sandu.voiceassistant.VoiceCallBack
import com.sandu.voiceassistant.VoiceInfo

class HomeFragment : BaseVBFragment<BaseViewModel, FragmentHomeBinding>() {

    lateinit var remoteVoiceInterface: IVoiceInterface
    private lateinit var voiceCallBack: MyVoiceCallBack
    override fun initView(savedInstanceState: Bundle?) {

        val intent = Intent()
        Log.d(TAG, "1111")
        intent.setAction("com.sandu.voiceassistant.IMyService")
        intent.setPackage("com.sandu.voiceassistant")
        activity?.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)

        intent.setAction("com.sandu.voiceservice")
        intent.setPackage("com.sandu.voiceassistant")
        activity?.bindService(intent, voiceServiceConnection, Context.BIND_AUTO_CREATE)
        Log.d(TAG, "2222")
        voiceCallBack = MyVoiceCallBack()



    }

    private val serviceConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            val remoteInterface: IStudentInterface = IStudentInterface.Stub.asInterface(service)
            try {
                //in 表示数据只能由客户端流向服务端。（表现为服务端修改此参数，不会影响客户端的对象）
                val studentInfoIn = StudentInfo()
                studentInfoIn.setName("client")
                Log.i("testClient", "name=" + remoteInterface.getConvertName(studentInfoIn))
                // name=client 不会改变
                Log.i("testClient", "name=" + studentInfoIn.getName())


                Log.i("testClient", "-----------------------------------------------")
                //out 表示数据只能由服务端流向客户端。（表现为服务端收到的参数是空对象，并且服务端修改对象后客户端会同步变动）
                val studentInfoOut: StudentInfo = StudentInfo()
                studentInfoOut.setId("200+")
                studentInfoOut.setName("clientOut")
                remoteInterface.getServiceStudentInfo(studentInfoOut)
                Log.i("testClient", "getServiceStudentInfo = $studentInfoOut")
                Log.i("testClient", "-----------------------------------------------")
                //inout 则表示数据可在服务端与客户端之间双向流通。（表现为服务端能接收到客户端传来的完整对象，并且服务端修改对象后客户端会同步变动）
                val studentInfoInOut: StudentInfo = StudentInfo()
                studentInfoInOut.setId("2001")
                studentInfoInOut.setName("clientInOut")
                remoteInterface.getServiceStudentInfoInOut(studentInfoInOut)
                Log.i("testClient", "getServiceStudentInfoInOut = $studentInfoInOut")
                Log.i("testClient", "-----------------------------------------------")
                //双向通信
                remoteInterface.setCallback(CallbackeService())
                val studentId: Int = remoteInterface.changeStudentId("helloworld")
                Log.i("testClient", "getStudentId =$studentId")
                val studentId1: Int = remoteInterface.changeStudentId("55")
                Log.i("testClient", "getStudentId1 =$studentId1")
                Log.i("testClient", "-----------------------------------------------")
                //linkToDeath
                remoteInterface.testLinkToDeath(CallbackeService())
                //主动延迟抛出一个异常来实现销毁服务端的目的
                Handler().postDelayed({ val i = 1 / 20 }, 5000)
            } catch (e: RemoteException) {
                Log.i("testClient", "RemoteException =" + e.message)
                e.printStackTrace()
            }
        }

        override fun onServiceDisconnected(name: ComponentName) {
            Log.i("testClient", "onServiceDisconnected=$name")
        }
    }

    private val voiceServiceConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            Log.i("testClient1", "onServiceDisconnected=$name")
            remoteVoiceInterface = IVoiceInterface.Stub.asInterface(service)
            mViewBind.textHome.setOnClickListener {
                val voiceInfoIn = VoiceInfo();
                voiceInfoIn.voiceMsg = "this is a aidl msg"
                voiceInfoIn.type = "addFirst"

                remoteVoiceInterface.sendVoice(voiceInfoIn, voiceCallBack)
            }
        }

        override fun onServiceDisconnected(name: ComponentName?) {
            Log.i("testClient2", "onServiceDisconnected=$name")
        }

    }

    class CallbackeService : IChangeCallback.Stub() {
        @Throws(RemoteException::class)
        override fun changeData(changeIndex: Int): Int {
            Log.i("testClient", "changeIndex = $changeIndex")
            return changeIndex + 1
        }
    }

    class MyVoiceCallBack:VoiceCallBack.Stub(){
        override fun onSuccess(s: String?) {
            Log.d("aidltest success", s + "")
        }

        override fun onError(s: String?) {
            Log.d("aidltest fail", s + "")
        }

    }
}
